﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RobotParser
{
    public enum TokenType : sbyte
    {
        EOF = - 127,
        ERROR = -1,        
        START = 0,
        STOP,
        LEFT, RIGHT,
        ON_45,
        HANDS_UP,
        HANDS_DOWN,
        TURN_HEAD,
        INTEGER,
        STEP_LPAREN, RPAREN
    }

    class Token
    {
        public TokenType Type { get; set; }
        public string Attribute { get; set; }

        public Token(TokenType type, string value  = "")
        {
            this.Type = type;
            this.Attribute = value;              
        }

        override public string ToString()
        {
            string optPart = (this.Attribute.Length == 0) ? "" : ", \"" + this.Attribute + "\"";
            return "<" + this.Type + optPart + ">";
        }
    }

    static class KeywordTokenCreator
    {
        private static Dictionary<string, TokenType> keywords = new Dictionary<string, TokenType>() 
        {
            {"left",       TokenType.LEFT},
            {"right",      TokenType.RIGHT},
            {"start",      TokenType.START},
            {"stop",       TokenType.STOP},
            {"hands_up",   TokenType.HANDS_UP},
            {"hands_down", TokenType.HANDS_DOWN},
            {"step_(",     TokenType.STEP_LPAREN},
            {"turn_head",  TokenType.TURN_HEAD},
            {"on45",       TokenType.ON_45}
        };

        public static Token CreateByString(string strKeyword)
        {
            TokenType type = keywords.ContainsKey(strKeyword) ? keywords[strKeyword] 
                                                              : TokenType.ERROR;            
            return new Token(type, strKeyword);
        }
    }
}
